package cn.web.tunnelrobot.controller;

import cn.web.tunnelrobot.dto.FileDTO;
import cn.web.tunnelrobot.dto.FileUpdate;
import cn.web.tunnelrobot.pojo.File;
import cn.web.tunnelrobot.service.FileService;
import cn.web.tunnelrobot.util.Constants;
import cn.web.tunnelrobot.util.UploadImage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 文件
 * @version 1.0.0
 *
 */
@RestController
@RequestMapping("/file")
@Api(tags = "文件管理接口")
public class FileController {
    @Autowired
    private FileService fileService;

    @PostMapping("/uploadFile")
    @ResponseBody
    @ApiOperation(value = "上传文件",notes = "上传文件")
    public HashMap<String,Object> uploadFile(@RequestParam(value = "file")MultipartFile file){
        HashMap<String, Object> map = new HashMap<>();

        FileDTO file1 = UploadImage.upLoadFile1(file);
        if (file1 != null){
            map.put("data",file1);
            map.put("code",0);
        }else {
            map.put("msg","上传失败");
            map.put("code",-1);
        }
        return map;
    }

    @PostMapping("/addFile")
    @ResponseBody
    @ApiOperation(value = "在数据库中添加文件信息",notes = "添加文件")
    public HashMap<String,Object> addFile(@RequestBody FileUpdate fileUpdate){
        return fileService.uploadFile(fileUpdate);
    }
    @PostMapping("/upload")
    @ResponseBody
    @ApiOperation(value = "上传文件",notes = "上传文件")
    public HashMap<String,Object> upload(@RequestParam(value = "file")MultipartFile file){
//        System.out.println("size:"+fileList.size());
        HashMap<String, Object> map = new HashMap<>();
        if(Constants.is_intranet == 1){
            map.put("code",-1);
            map.put("msg","外网没有权限");
            return map;
        }
        String url = UploadImage.upLoadFile(file);
        String filename = file.getOriginalFilename();//原始文件名
        if (url != null){
            map.put("url",url);
            map.put("name",filename);
            map.put("code",0);
        }else {
            map.put("msg","上传失败");
            map.put("code",-1);
        }
        return map;
    }
    @PostMapping("/updateFile")
    @ResponseBody
    public HashMap<String,Object> updateFile(@RequestBody File File){
        return fileService.updateFile(File);
    }
    @GetMapping("/getFileList")
    @ResponseBody
    @ApiOperation(value = "按条件查询文件列表",notes = "查询文件")
    public HashMap<String,Object> getFileList(@RequestParam(required = false) Integer fiType,
                                              @RequestParam(required = false) String fiMileage,
                                              @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                                              @RequestParam(value = "pageSize",defaultValue = "20") Integer pageSize){
        return fileService.getFileList(fiType,fiMileage,pageNo,pageSize);
    }
    @PostMapping("/uploadVoiceFile")
    @ResponseBody
    public HashMap<String,Object> uploadVoiceFile(@RequestParam(value = "file")MultipartFile file){
        HashMap<String, Object> map = new HashMap<>();
        if(Constants.is_intranet == 1){
            map.put("code",-1);
            map.put("msg","外网没有权限");
            return map;
        }
        FileDTO file1 = UploadImage.upLoadVoiceFile(file);
        if (file1 != null){
            map.put("data",file1);
            map.put("code",0);
        }else {
            map.put("msg","上传失败");
            map.put("code",-1);
        }
        return map;
    }
    @PostMapping("/voiceIntercom")
    @ResponseBody
    public Object voiceIntercom(@RequestParam(value = "file")MultipartFile file){
        String result = "";
        String s = UploadImage.upLoadFile(file);
        try {
            result = Constants.doGet(Constants.voiceIntercom+"?file="+Constants.fwqAddress+s);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    @PostMapping("/videoIntercom")
    @ResponseBody
    public Object videoIntercom(@RequestParam(value = "file")MultipartFile file){
        HashMap<String,Object> map = new HashMap<>();
        if(Constants.is_intranet == 1){
            map.put("code",-1);
            map.put("msg","外网没有权限");
            return map;
        }
        String s = UploadImage.upLoadFile(file);
        return s;
    }
}
